# coding=utf-8


import xlrd

from persistents import RedisServices


class DevicesManagerTask(object):

    def __init__(self):
        self.device_key = "devices_info"
        self.rds_handler = RedisServices()

    def __call__(self, *args, **kwargs):
        self.parse_excel(*args, **kwargs)

    def parse_excel(self, path_str=r'./configs/devices_info.xls'):
        data = xlrd.open_workbook(path_str)
        table = data.sheet_by_index(0)
        # print table.nrows, table.ncols
        devicesinfo = {}
        for row_idx in xrange(table.nrows):
            row = table.row_values(row_idx)
            if row[1] in devicesinfo.keys():
                devicesinfo[row[1]].append(row[-1])
            else:
                devicesinfo[row[1]] = [row[-1], ]
        self.rds_handler.set(self.device_key, str(devicesinfo))

    def get_device_version(self, device_name):
        device_info = eval(self.rds_handler.get(self.device_key))
        device_name_list = []
        device_name_list.extend(device_name)
        device_info_dict = {}
        for d in device_name_list:
            device_info_dict[d] = device_info[d]
        return device_info_dict


if __name__ == '__main__':
    DevicesManagerTask()()
